package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zyx
 * @CreateTime: 2024-10-10
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 新增菜品和口味
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
//        获取菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        插入一条菜品
        dishMapper.insert(dish);
        Long id = dish.getId();
//        添加多条口味
//        先获得口味列表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分类查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.queryPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品批量删除
     * @param ids
     * @return
     */
    @Override
    public void deleteBatch(List<Long> ids) {
//        起售不能删除
//        1.通过传过来的ids拿到对应菜品的状态
        for (Long id :ids) {
           Dish dish =  dishMapper.getById(id);
            //        2.判断状态
            if(dish.getStatus() == StatusConstant.ENABLE){
//                抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//        套餐里不能删除 多对多
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
//            不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//        批量删除菜品
        dishMapper.deleteId(ids);
//        批量删除菜品相关的口味
        dishFlavorMapper.deleteByDishIds(ids);

    }
    /**
     * 根据id查到菜品和口味
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
//        先通过id查到菜品
        Dish dish = dishMapper.getById(id);
//        通过菜品id查到对应的口味id
        List<DishFlavor> dishFlavors = dishFlavorMapper.getFlavorIDByDishId(id);
//        将查到的拼接给Vo
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        BeanUtils.copyProperties(dishFlavors,dishVO);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
//        先拿到dish 更新菜品的基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
//        先删除口味

        dishFlavorMapper.deleteByDishId(dishDTO.getId());
//        再插入新的口味
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}
